﻿using System;
using System.Linq;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Collections.Generic;
///
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using ScheduleMVVM.Model.PersistentFramework;
using ScheduleMVVM.Model.RuntimeFramework;
using ScheduleMVVM.Model.Miscellaneous;
using ScheduleMVVM.Localization;

namespace ScheduleMVVM.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SearchViewModel : ViewModelBase
    {
        #region Binded properties
        public String ApplicationTitle
        {
            get { return GeneralResource.ApplicationTitle.ToUpper(); }
        }
        public String DepartmentString
        {
            get { return GeneralResource.DepartmentString; }
        }
        public String DepartureString
        {
            get { return GeneralResource.DepartureString; }
        }
        public String ArrivalString
        {
            get { return GeneralResource.ArrivalString; }
        }
        public String SearchPageTitle
        {
            get { return GeneralResource.SearchPageTitle.ToLower(); }
        }
        public String[] Departments
        {
            get { return AdvancedRuntimeContext.Current.Departments; }
        }
        private Int32 _selectedindex;
        public Int32 SelectedIndex
        {
            get
            {
                return _selectedindex;
            }
            set
            {
                _selectedindex = value;
                RaisePropertyChanged("DepartmentStations");
            }
        }
        public ObservableCollection<StationEntry> DepartmentStations
        {
            get
            {
                return new ObservableCollection<StationEntry>(AdvancedRuntimeContext.Current.Stations.Where(
                    x => x.Department == Departments[SelectedIndex]
                    ));
            }
        }
        public List<StationEntry> Stations
        {
            get
            {
                return new List<StationEntry>(AdvancedRuntimeContext.Current.Stations);
            }
        }
        public String DepartureStationString
        { get; set; }
        public String ArrivalStationString
        { get; set; }
        public StationEntry DepartureStation
        { get; set; }
        public StationEntry ArrivalStation
        { get; set; }
        public RelayCommand SearchCommand
        {
            get
            {
                return new RelayCommand(delegate
                    {
                        try
                        {
                            var favourite = new ScheduleMVVM.Model.RuntimeFramework.RouteEntry();
                            if (ArrivalStation != null) favourite.endStation = ArrivalStation;
                            else if (ArrivalStationString != null)
                            {
                                var collection = AdvancedRuntimeContext.Current.Stations.Where(x => x.Title.ToLower().Contains(ArrivalStationString.ToLower()));

                                if (collection.Count() > 1)
                                {
                                    System.Windows.MessageBox.Show(GeneralResource.DropdownMenuReminder);
                                    return;
                                }
                                else favourite.endStation = collection.First();
                            }
                            else
                            {
                                System.Windows.MessageBox.Show(GeneralResource.DropdownMenuReminder);
                                return;
                            }
                            if (DepartureStation != null) favourite.startStation = DepartureStation;
                            else if (DepartureStationString != null)
                            {
                                var collection = AdvancedRuntimeContext.Current.Stations.Where(x => x.Title.ToLower().Contains(DepartureStationString.ToLower()));
                                if (collection.Count() > 1)
                                {
                                    System.Windows.MessageBox.Show(GeneralResource.DropdownMenuReminder);
                                    return;
                                }
                                else favourite.startStation = collection.First();
                            }
                            else
                            {
                                System.Windows.MessageBox.Show(GeneralResource.DropdownMenuReminder);
                                return;
                            }
                            favourite.IsSearch = true;
                            Messenger.Default.Send<UpdateRouteViewCommand>(new UpdateRouteViewCommand(new RouteEntryWrapper(favourite)));
                            ((App)App.Current).RootFrame.Navigate(new System.Uri("/RoutePage.xaml", System.UriKind.Relative));
                            EmptyFields();
                        }
                        catch
                        {
                            System.Windows.MessageBox.Show(GeneralResource.DropdownMenuReminder);
                        }
                    });
            }
        }
        private void EmptyFields()
        {
            DepartureStationString = String.Empty;
            ArrivalStationString = String.Empty;
            DepartureStation = null;
            ArrivalStation = null;
            //RaisePropertyChanged("");
        }
        #endregion
        /// <summary>
        /// Initializes a new instance of the SearchViewModel class.
        /// </summary>
        public SearchViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                _selectedindex = 0;
            }
            else
            {
                _selectedindex = 0;
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}